package com.csxy.m2020_note;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class NotesAdapter  extends BaseAdapter {

    Context mContext;//上下文对象
    int layoutId;//布局id
    List<Notebean> myList;//数据源

    public NotesAdapter(Context mContext, int layoutId, List<Notebean> myList) {
        this.mContext = mContext;
        this.layoutId = layoutId;
        this.myList = myList;
    }

    @Override
    public int getCount() {
        return myList.size();
    }

    @Override
    public Object getItem(int position) {
        return myList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //将布局文件id解析为视图
        convertView = LayoutInflater.from(mContext).inflate(layoutId,parent,false);

        //绑定view中的控件
        TextView titleText = (TextView) convertView.findViewById(R.id.title);
        TextView bodyText=(TextView)convertView.findViewById(R.id.body);

        //为TextView控件设置相对应的文本
        Notebean m=myList.get(position);
        titleText.setText(""+m.getTitle());
        bodyText.setText(""+m.getBody());


        //为布局设置文本数据后返回，这个convertView就是ListView的子项
        return convertView;

    }
}
